﻿using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace OpenSearchTests
{
    [TestClass]
    public class NasaEcho
    {
        [TestMethod]
        [DeploymentItem("nasa_echo_osdd.xml")]
        public void TestParseNasaEchoOsdd()
        {
            System.IO.FileStream stream = new System.IO.FileStream("nasa_echo_osdd.xml", System.IO.FileMode.Open);
            OpenSearch.Osdd.OpenSearchDescriptionDocument osdd = new OpenSearch.Osdd.OpenSearchDescriptionDocument(stream);

            Assert.AreEqual("NASA ECHO Dataset Open Search", osdd.ShortName);

            Assert.AreEqual("Open Search compliant NASA ECHO Dataset search that responds in the ATOM format with Open Search, GeoRSS, Open Search Time and ECHO extensions.", osdd.Description);

            List<string> expectedTags = new List<string> { "ESIP", "ECHO", "NASA" };
            Assert.IsTrue(expectedTags.SequenceEqual(osdd.Tags));

            Assert.AreEqual("support@echo.nasa.gov", osdd.Contact);

            List<OpenSearch.Osdd.Url> urls = osdd.Urls.ToList();
            Assert.AreEqual(2, urls.Count);
            OpenSearch.Osdd.Url url0 = urls[0];
            Assert.AreEqual(1, url0.IndexOffset);
            Assert.AreEqual("application/atom+xml", url0.MimeType);
            Assert.AreEqual("https://api.echo.nasa.gov:443/opensearch/datasets.atom?keyword={os:searchTerms?}&instrument={echo:instrument?}&satellite={echo:satellite?}&boundingBox={geo:box?}&geometry={geo:geometry?}&placeName={geo:name?}&startTime={time:start?}&endTime={time:end?}&cursor={os:startPage?}&numberOfResults={os:count?}&uid={geo:uid?}&clientId=opensearchnet", url0.Template.ToString());
            OpenSearch.Osdd.Url url1 = urls[1];
            Assert.AreEqual(1, url1.IndexOffset);
            Assert.AreEqual("text/html", url1.MimeType);
            Assert.AreEqual("https://api.echo.nasa.gov:443/opensearch/datasets.html?keyword={os:searchTerms?}&instrument={echo:instrument?}&satellite={echo:satellite?}&boundingBox={geo:box?}&geometry={geo:geometry?}&placeName={geo:name?}&startTime={time:start?}&endTime={time:end?}&cursor={os:startPage?}&numberOfResults={os:count?}&uid={geo:uid?}&clientId=opensearchnet", url1.Template.ToString());

            List<OpenSearch.Query> queries = osdd.Queries.ToList();
            Assert.AreEqual(1, queries.Count);
            var q = queries[0];

            OpenSearch.Query query = queries[0];
            Assert.AreEqual("example", query.Role);
            Assert.AreEqual("Sample search", query.Title);

            // Not present.
            Assert.AreEqual("", osdd.Developer);

            Assert.AreEqual("open", osdd.SyndicationRight.ToString());

            Assert.IsFalse(osdd.AdultContent);
        }

        [TestMethod]
        [DeploymentItem("nasa_echo_osdd.xml")]
        public void TestParseNasaEchoOsddStringReader()
        {
            string osddText = System.IO.File.ReadAllText("nasa_echo_osdd.xml");
            OpenSearch.Osdd.OpenSearchDescriptionDocument osdd = new OpenSearch.Osdd.OpenSearchDescriptionDocument(osddText);

            Assert.AreEqual("NASA ECHO Dataset Open Search", osdd.ShortName);

            Assert.AreEqual("Open Search compliant NASA ECHO Dataset search that responds in the ATOM format with Open Search, GeoRSS, Open Search Time and ECHO extensions.", osdd.Description);

            List<string> expectedTags = new List<string> { "ESIP", "ECHO", "NASA" };
            Assert.IsTrue(expectedTags.SequenceEqual(osdd.Tags));

            Assert.AreEqual("support@echo.nasa.gov", osdd.Contact);

            List<OpenSearch.Osdd.Url> urls = osdd.Urls.ToList();
            Assert.AreEqual(2, urls.Count);
            OpenSearch.Osdd.Url url0 = urls[0];
            Assert.AreEqual(1, url0.IndexOffset);
            Assert.AreEqual("application/atom+xml", url0.MimeType);
            Assert.AreEqual("https://api.echo.nasa.gov:443/opensearch/datasets.atom?keyword={os:searchTerms?}&instrument={echo:instrument?}&satellite={echo:satellite?}&boundingBox={geo:box?}&geometry={geo:geometry?}&placeName={geo:name?}&startTime={time:start?}&endTime={time:end?}&cursor={os:startPage?}&numberOfResults={os:count?}&uid={geo:uid?}&clientId=opensearchnet", url0.Template.ToString());
            OpenSearch.Osdd.Url url1 = urls[1];
            Assert.AreEqual(1, url1.IndexOffset);
            Assert.AreEqual("text/html", url1.MimeType);
            Assert.AreEqual("https://api.echo.nasa.gov:443/opensearch/datasets.html?keyword={os:searchTerms?}&instrument={echo:instrument?}&satellite={echo:satellite?}&boundingBox={geo:box?}&geometry={geo:geometry?}&placeName={geo:name?}&startTime={time:start?}&endTime={time:end?}&cursor={os:startPage?}&numberOfResults={os:count?}&uid={geo:uid?}&clientId=opensearchnet", url1.Template.ToString());

            List<OpenSearch.Query> queries = osdd.Queries.ToList();
            Assert.AreEqual(1, queries.Count);
            var q = queries[0];

            OpenSearch.Query query = queries[0];
            Assert.AreEqual("example", query.Role);
            Assert.AreEqual("Sample search", query.Title);

            // Not present.
            Assert.AreEqual("", osdd.Developer);

            Assert.AreEqual("open", osdd.SyndicationRight.ToString());

            Assert.IsFalse(osdd.AdultContent);
        }

        [TestMethod]
        [DeploymentItem("echo_response.xml")]
        public void TestParseNasaEchoResponse()
        {
            string responseText = System.IO.File.ReadAllText("echo_response.xml");
            OpenSearch.Response response = new OpenSearch.Response(responseText);

            Assert.AreEqual("ECHO dataset metadata", response.Title);
            List<OpenSearch.Query> queries = response.Queries.ToList<OpenSearch.Query>();
            Assert.AreEqual(1, queries.Count);
            OpenSearch.Query query = queries[0];
            Assert.IsNotNull(query);
            Assert.AreEqual("request", query.Role);
            Assert.IsNull(query.SearchTerms);
        }
    }
}